<?php

class Video_Plugin_Service_Vimeo
{

	function getName()
	{
		return 'vimeo';
	}


	function getVideoInfo($link)
	{
		$return =  array();

		$params = @pathinfo($link);
		$code = $params['basename'];

		if (!$code)
		{
			return NULL;
		}

		$apiLink = "http://vimeo.com/api/v2/video/$code.json";

		$curl = new Nuo_Curl();

		$response = $curl -> send($apiLink);

		$data = json_decode($response, 1);

		if (!is_array($data))
		{
			return NULL;
		}

		$data = $data[0];

		if (!$data or !isset($data['id']))
		{
			return NULL;
		}

		$return['title'] = $data['title'];
		$return['source_id'] = $code;
		$return['source_mobile_url'] = $data['mobile_url'];
		$return['duration'] = (int)$data['duration'];
		$desc = $data['description'];
		$desc = substr(strip_tags($desc), 0, 250);
		if ('' == $desc)
		{
			$return['description'] = $data['tags'];
		}
		else
		{
			$return['description'] = $desc;
		}
		$return['thumbnail_large'] = $data['thumbnail_large'];

		return $return;

	}

	function getEmbedCode($item, $params)
	{
		$sourceId = $item -> source_id;
		$view = 1;
		$string = <<<THESTRING
<object width=":width" height=":height">
<param name="allowfullscreen" value="true"/>
<param name="allowscriptaccess" value="always"/>
<param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=:clipId&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" />
<embed src="http://vimeo.com/moogaloop.swf?clip_id=:clipId&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1&amp;autoplay=:autoplay" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width=":width" height=":height" wmode="transparent"/>
<param name="wmode" value="transparent" />
</object>
THESTRING;

		return strtr($string, array(
			':width' => '425',
			':height' => '344',
			':clipId' => $item -> source_id,
			':autoplay' => 0
		));
	}

}
